var boxs = new Array();
var arrY = new Array();


function Box(boxNum) {
    this.BoxName = 'd'+boxNum;
    this.BoxValue = '';
    this.ChangeText = function (value) {
        this.BoxValue = value;
        document.getElementById(this.BoxName).innerHTML = value;
    };
}

//随机生成一个整数
function RandomNum(minNum,maxNum){ 
    switch(arguments.length){ 
        case 1: 
            return parseInt(Math.random()*minNum+1,10); 
        case 2: 
            return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10); 
            default: 
                return 0; 
    } 
}

//随机生成1个2或者4的数字
function RanBox() {
    try {
        IsFail();
        var Get = false;
        while (!Get) {
            var ranBox = RandomNum(0,15);
            if (!boxs[ranBox].BoxValue && boxs[ranBox].BoxValue=='') {
                Get = true;
                boxs[ranBox].ChangeText(RandomNum(1,2)*2);
            }
        }
    } catch (error) {
        throw error;
    }

}

//判断是否失败
function IsFail() {
    try {
        var flag = false;
        boxs.forEach(box => {
            if (!box.BoxValue && box.BoxValue=='') {
                flag = true;
            }
        });
        if (!flag) {
            throw "游戏失败";
        }
    } catch (error) {
        throw error;
    }

}

function KeyDownAfter() {
    try {
        RanBox();
    } catch (error) {
        throw error;
    }
    
}

